package cqie.personal.frame.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import cqie.personal.frame.bo.AddModifyBO;
import cqie.personal.frame.bo.GetBO;
import cqie.personal.frame.bo.ModifyCourseBO;
import cqie.personal.frame.entity.ClassInfo;
import cqie.personal.frame.entity.CourseInfo;
import cqie.personal.frame.entity.UserInfo;
import cqie.personal.frame.exception.ValidationFailException;
import cqie.personal.frame.mapper.ClassInfoMapper;
import cqie.personal.frame.mapper.CourseInfoMapper;
import cqie.personal.frame.mapper.UserInfoMapper;
import cqie.personal.frame.service.CourseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cqie.personal.frame.vo.PageVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yeeeeee
 * @since 2021-09-28
 */
@Service
public class CourseInfoServiceImpl extends ServiceImpl<CourseInfoMapper, CourseInfo> implements CourseInfoService {
    @Resource
    private CourseInfoMapper courseInfoMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private ClassInfoMapper classInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_UNCOMMITTED)
    public synchronized boolean addCourse(AddModifyBO addModifyBO) throws ValidationFailException {
        if(addModifyBO==null){
            throw new ValidationFailException("Fehler server");
        }
        if(!addModifyBO.Validate()){
            return false;
        }
        QueryWrapper<CourseInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_name",addModifyBO.getName());
        if(courseInfoMapper.selectCount(queryWrapper)>0){
            throw new ValidationFailException("Der name wird besessen.");
        }
        CourseInfo courseInfo = new CourseInfo();
        courseInfo.setCourseName(addModifyBO.getName());
        Date date = new Date();
        courseInfo.setCreatePerson(addModifyBO.getOperator());
        courseInfo.setCreateTime(date);
        courseInfo.setLastUpgradePerson(addModifyBO.getOperator());
        courseInfo.setLastUpgradeTime(date);
        return courseInfoMapper.insert(courseInfo)==1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_UNCOMMITTED)
    public synchronized boolean modifyCourse(AddModifyBO addModifyBO) throws ValidationFailException {
        if(addModifyBO == null){
            throw  new ValidationFailException("服务器错误");
        }
        if(!addModifyBO.Validate()){
            return false;
        }
        if(addModifyBO.getId()==null){
            throw new ValidationFailException("未获取到修改对象");
        }
        QueryWrapper<CourseInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",addModifyBO.getId());
        CourseInfo courseInfo =  courseInfoMapper.selectOne(queryWrapper);
        if(courseInfo == null){
            throw new ValidationFailException("修改对象不可用");
        }
        queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id",addModifyBO.getId());
        queryWrapper.eq("course_name",addModifyBO.getName());
        if(courseInfoMapper.selectCount(queryWrapper)>0){
            throw new ValidationFailException("名称被占用");
        }
        courseInfo.setCourseName(addModifyBO.getName());
        if(addModifyBO.getAbandon()!=null){
            courseInfo.setCourseAbandon(addModifyBO.getAbandon());
        }
        UpdateWrapper<CourseInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",addModifyBO.getId());
        return courseInfoMapper.update(courseInfo,updateWrapper)==1;
    }

    @Override
    public PageVO<List<CourseInfo>> getCourse(GetBO getBO) throws ValidationFailException {
        if(getBO==null){
            throw new ValidationFailException("伺服器錯誤");
        }
        QueryWrapper<CourseInfo> queryWrapper = new QueryWrapper<>();
        if(getBO.getId()!=null){
            queryWrapper.eq("id",getBO.getId());
        }
        if(getBO.getName()!=null){
            queryWrapper.like("course_name",getBO.getName());
        }
        queryWrapper.last("LIMIT "+getBO.getPageOffSet()+","+getBO.getPageSize());

        List<CourseInfo> data = courseInfoMapper.selectList(queryWrapper);
        PageVO<List<CourseInfo>> result = new PageVO<>(data);
        result.setPageNo(getBO.getPageNo());
        result.setPageSize(getBO.getPageSize());
        result.setRecordCount(data.size());
        queryWrapper.last("");
        Integer totalCount = courseInfoMapper.selectCount(queryWrapper);
        if(totalCount == null){
            result.setTotalCount(0);
            result.setTotalPage(0);
        }else {
            result.setTotalCount(totalCount);
            result.setTotalPage(result.getTotalCount() % result.getPageSize() == 0 ?
                    result.getTotalCount() / result.getPageSize() :
                    result.getTotalCount() / result.getPageSize() + 1);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_UNCOMMITTED)
    public synchronized boolean modifyCourseTable(ModifyCourseBO modifyBO) throws ValidationFailException {
        if(modifyBO==null){
            throw new ValidationFailException("something go wrong");
        }
        modifyBO.validation();
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("uid",modifyBO.getUid());
        UserInfo userInfo = userInfoMapper.selectOne(userInfoQueryWrapper);
        if(userInfo == null){
            throw new ValidationFailException("user do not exits");
        }
        if(userInfo.getUserRole()!=1){
            throw new ValidationFailException("not teacher");
        }
        QueryWrapper<ClassInfo> classInfoQueryWrapper = new QueryWrapper<>();
        classInfoQueryWrapper.eq("id",modifyBO.getClassId());
        if(classInfoMapper.selectCount(classInfoQueryWrapper)<1){
            throw new ValidationFailException("class unable");
        }
        QueryWrapper<CourseInfo> courseInfoQueryWrapper = new QueryWrapper<>();
        classInfoQueryWrapper.eq("id",modifyBO.getCourseId());
        if(courseInfoMapper.selectCount(courseInfoQueryWrapper)<1){
            throw new ValidationFailException("course unable");
        }
        return courseInfoMapper.addCourseTable(modifyBO.getOperator(),new Date(),modifyBO.getUid(),modifyBO.getClassId(),modifyBO.getCourseId())==1;
    }

    @Override
    public PageVO<List<CourseInfo>> getClassCourse(GetBO getBO) throws ValidationFailException {
        if(getBO==null){
            throw new ValidationFailException("获取参数失败");
        }
        if(getBO.getId()==null){
            throw new ValidationFailException("获取班级信息失败");
        }
        List<CourseInfo> data = courseInfoMapper.getCourseClass(getBO.getId());
        PageVO<List<CourseInfo>> result =new PageVO<>(data);
        result.setRecordCount(data.size());
        return result;
    }


}
